<template>
  <div class="register-box">
    <div class="register-logo">
      <img src="../assets/img/logo.png" alt>
    </div>
    <div class="register-form">
      <div class="register-field">
        <i class="field-icon field-icon-account"></i>
        <input type="text" v-model.trim="Phone" maxlength="11" placeholder="请输入手机号">
      </div>
      <div class="register-field">
        <i class="field-icon field-icon-password"></i>
        <input type="password" v-model="Password" placeholder="请输入登录密码">
      </div>
      <button class="register-btn" @click="LoginByPassword">绑定微信</button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'wxBindPage',
  data() {
    return {
      Phone: '',
      Password: ''
    };
  },
  created() {
      if (this.$route.query.token) {
          this.BindWeixin(this.$route.query.token);
      }
  },
  watch: {
    $route: {
        handler: function(val, oldVal) {
            if (val.query.token) {
                this.BindWeixin(val.query.token);
            }
        },
        deep: true
    }
  },
  methods: {
    BindWeixin(token) {
      var that = this;
      $.ajax({
        type: 'POST',
        url: `${that.baseUrl}/api/account/BindingWeixin`,
        headers: { Authorization: localStorage['token'] },
        data: {
            Token: token
        },
        success: function(res) {
            if (res.State) {
                window.location.href = '#/';
            }
        }
      });
    },
    LoginByPassword() {
      if (this.Phone == '') {
        this.$toast('请输入手机号');
      } else if (this.Password == '') {
        this.$toast('请输入登录密码');
      } else {
        var that = this;
        $.ajax({
          type: 'POST',
          url: `${that.baseUrl}/api/Register/login`,
          data: {
            Account: that.Phone,
            Password: that.Password
          },
          success: function(res) {
            console.log(res);
            if (res.State) {
                that.$cookies.set('token', res.Data, 8 * 60 * 60);
                localStorage.setItem('token', res.Data);
                that.$store.commit('SET_TOKEN', res.Data);
              window.location.href = `http://es.11cm.com.cn/WeiXinManage/GetWeiXinOpenID?ST=test&NotRegister=true&ReferrerUrl=${encodeURIComponent(
                window.location.href
              )}`;
            }
          }
        });
      }
    }
  }
};
</script>

<style>
</style>
